//
//  ViewControllerForRadioCollectPlay.m
//  世界
//
//  Created by dllo on 16/3/2.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "ViewControllerForRadioCollectPlay.h"
#import <AVFoundation/AVFoundation.h>
#import <UIImageView+WebCache.h>
#import "DataBaseForRadio.h"
#import "ModelForRadioCollect.h"
#import "CellForRadioPlayList.h"
#import "NetworkingHandler.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewControllerForRadioCollectPlay ()<UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, retain) NSString *strForURL;

@property (nonatomic, retain) UITableView *tabView;



@property (nonatomic, retain) NSMutableArray *arrCover;

@property (nonatomic, retain) NSMutableArray *arrUrl_m3u8;

@property (nonatomic, retain) UIImageView *imgViewForMusic;

@property (nonatomic, retain) CABasicAnimation *animation;



@property (nonatomic, retain) UIButton *buttonForPlay;

@property (nonatomic, retain) UIButton *buttonForLast;

@property (nonatomic, retain) UIButton *buttonForNext;

@property (nonatomic, retain) UIButton *buttonForBack;

@property (nonatomic, retain) NSMutableArray *arrForCollect;

@property (nonatomic, retain) UIView *playerView;

@property (nonatomic,retain) AVPlayer *player;

@property (nonatomic,retain) AVPlayerItem *playerItem;

@property (nonatomic, assign) CGFloat totalMovieDuration; // 保存该视频资源的总时长，快进或快退的时候要用

@property (nonatomic, retain) UISlider *progressSlider;

@property (nonatomic, retain) UIProgressView *progressViewForRadioCache;

@property (nonatomic, retain) UILabel *labelForStarTime;

@property (nonatomic, retain) UILabel *lableForEndTime;

@property (nonatomic, retain) UILabel *lableForTitle;



@property (nonatomic, assign) BOOL isTap;

@property (nonatomic, assign) BOOL isOK;

@property (nonatomic, assign) BOOL isEqual;

@property (nonatomic, assign) BOOL isPlay;

@property (nonatomic, assign) BOOL isOver;

@property (nonatomic, assign) BOOL isSelect;



@property (nonatomic, assign) NSInteger cunt;

@property (nonatomic, assign) NSInteger equal;

@property (nonatomic, assign) NSInteger value;

@property (nonatomic, assign) NSInteger value1;

@property (nonatomic, assign) int num;



@property (nonatomic ,strong) id playbackTimeObserver;

@end

@implementation ViewControllerForRadioCollectPlay


#pragma mark 重写dealloc方法
- (void)dealloc
{
    [super dealloc];
    
    [_tabView release];
    
    [_arrCover release];
    
    [_arrUrl_m3u8 release];
    
    [_imgViewForMusic release];
    
    [_animation release];
    
    [_buttonForPlay release];
    
    [_buttonForLast release];
    
    [_buttonForNext release];
    
    [_buttonForBack release];
    
    [_playerView release];
    
    [_player release];
    
    [_playerItem release];
    
    [_progressSlider release];
    
    [_progressViewForRadioCache release];
    
    [_labelForStarTime release];
    
    [_lableForEndTime release];
    
    [_arrForCollect release];
    
    [self removeObservers];
}




#pragma mark 视图的生命周期 >>>>>>
#pragma mark 视图即将显示
- (void)viewWillAppear:(BOOL)animated
{
    [self setdiscImageView];
}



#pragma mark 视图加载完成
- (void)viewDidLoad
{
    self.cunt = 0;
    
    self.equal = 0;
    
    self.value = 0;
    
    self.value1 = 0;
    
    self.num = 0;
    
    [[DataBaseForRadio shareDataBaseHandle]open];
    
    self.arrForCollect = [[DataBaseForRadio shareDataBaseHandle] select];
    
    [super viewDidLoad];
    
    [self createTabView];
    
    [self createViews];
    
    [self addAVPlayer];
    
    //添加观察者
    [self addObservers];
    
    NSIndexPath *path = [NSIndexPath indexPathForRow:self.number inSection:0];

    [self tableView:self.tabView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:self.number inSection:0]];

    [self.tabView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}




#pragma mark 接受到内存警告 !!!
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



#pragma mark 自定义方法 >>>>>>
#pragma mark 处理数据 (界面)
- (void)handleData
{
    //设置访问地址
    
    ModelForRadioCollect *model = [self.arrForCollect objectAtIndex:self.number];
    
    self.strForURL = model.postid;
    
    NSString *url = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html",self.strForURL];
    
    [NetworkingHandler netWorkingWithURL:url completeHandle:^(NSURLResponse *response, NSData *data ,id result) {
        
        self.arrCover = [NSMutableArray array];
        
        self.arrUrl_m3u8 = [NSMutableArray array];
        
        NSDictionary *dic = [result objectForKey:model.postid];
        
        NSArray *arr = [dic objectForKey:@"video"];
        
        for (NSDictionary *dic1 in arr)
        {
            NSString *str1 = [dic1 objectForKey:@"cover"];
            
            [self.arrCover addObject:str1];
            
            NSString *str2 = [dic1 objectForKey:@"url_m3u8"];
            
            [self.arrUrl_m3u8 addObject:str2];
        }
            
            NSURL *url1=[NSURL URLWithString:self.arrUrl_m3u8[0]];
            
            //创建视频项目对象
            self.playerItem = [AVPlayerItem playerItemWithURL:url1];
            
            //添加观察者
            [self addObservers];
            
            [self.player replaceCurrentItemWithPlayerItem:self.playerItem];
            
            [self.imgViewForMusic sd_setImageWithURL:[NSURL URLWithString:self.arrCover[0]] placeholderImage:[UIImage imageNamed:@"碟片.jpg"]];
            
            [self.player play];
    }];
}





#pragma mark 创建子视图
- (void)createViews
{
    self.playerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 414, 300)];
    
    UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"音乐背景.jpg"]];
    
    [self.playerView setBackgroundColor:bgColor];
    
    [self.view addSubview:self.playerView];
    
    [_playerView release];
    
    
    
    
    self.lableForTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, self.view.frame.size.width - 40, 50)];
    
    self.lableForTitle.textAlignment = NSTextAlignmentCenter;
    
    [self.lableForTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:25]];
    
    [self.playerView addSubview:self.lableForTitle];
    
    [_lableForTitle release];
    
    
    
    
    
    self.labelForStarTime = [[UILabel alloc] initWithFrame:CGRectMake(0, 265, 100, 30)];
    
    self.labelForStarTime.text = @"00 : 00";
    
    self.labelForStarTime.textAlignment =  NSTextAlignmentCenter;
    
    [self.playerView addSubview:self.labelForStarTime];
    
    [_labelForStarTime release];
    
    
    
    
    
    self.lableForEndTime = [[UILabel alloc] initWithFrame:CGRectMake(314, 265, 100, 30)];
    
    self.lableForEndTime.text = @"00: 00";
    
    self.lableForEndTime.textAlignment =  NSTextAlignmentCenter;
    
    [self.playerView addSubview:self.lableForEndTime];
    
    [_lableForEndTime release];
    
    
    
    
    self.progressViewForRadioCache = [[UIProgressView alloc] init];
    
    self.progressViewForRadioCache.frame = CGRectMake(100, 279, SCREEN_WIDTH-200, 40);
    
    [self.playerView addSubview:self.progressViewForRadioCache];
    
    [_progressViewForRadioCache release];
    
    
    
    
    
    
    self.progressSlider = [[UISlider alloc] initWithFrame:CGRectMake(100, 260, self.view.frame.size.width-200, 40)];
    
    [self.progressSlider addTarget:self action:@selector(ProgressAction:) forControlEvents:UIControlEventValueChanged];
    
    UIGraphicsBeginImageContextWithOptions((CGSize){ 1, 1 }, NO, 0.0f);
    
    UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [self.progressSlider setMinimumTrackImage:transparentImage forState:UIControlStateNormal];
    
    [self.progressSlider setMaximumTrackImage:transparentImage forState:UIControlStateNormal];
    
    
    [self.playerView addSubview:self.progressSlider];
    
    [_progressSlider release];
    
    
    
    
    
    self.imgViewForMusic = [[UIImageView alloc] initWithFrame:CGRectMake(137, 100, 150, 150)];
    
    self.imgViewForMusic.layer.borderWidth = 3;
    
    self.imgViewForMusic.layer.borderColor = [[UIColor orangeColor] CGColor];
    
    self.imgViewForMusic.image = [UIImage imageNamed:@"碟片.jpg"];
    
    self.imgViewForMusic.layer.cornerRadius = 75;
    
    self.imgViewForMusic.layer.masksToBounds = YES;
    
    [self.playerView addSubview:self.imgViewForMusic];
    
    [_imgViewForMusic release];
    
    
    
    
    
    self.buttonForBack = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [self.buttonForBack setBackgroundImage:[UIImage imageNamed:@"后退"] forState:UIControlStateNormal];
    
    [self.buttonForBack addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.buttonForBack.frame = CGRectMake(20, 25, 40, 40);
    
    [self.playerView addSubview:self.buttonForBack];
    
    [_buttonForBack release];
    
    
    
    
    
    self.buttonForPlay = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [self.buttonForPlay setBackgroundImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
    
    [self.buttonForPlay addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.buttonForPlay.frame = CGRectMake(187, 150, 50, 50);
    
    [self.playerView addSubview:self.buttonForPlay];
    
    [_buttonForPlay release];
    
    
    
    
    
    self.buttonForLast = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [self.buttonForLast setBackgroundImage:[UIImage imageNamed:@"上一曲"] forState:UIControlStateNormal];
    
    [self.buttonForLast addTarget:self action:@selector(ListAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.buttonForLast.frame = CGRectMake(40, 150, 50, 50);
    
    [self.playerView addSubview:self.buttonForLast];
    
    [_buttonForLast release];
    
    
    
    
    
    self.buttonForNext = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [self.buttonForNext setBackgroundImage:[UIImage imageNamed:@"下一曲"] forState:UIControlStateNormal];
    
    [self.buttonForNext addTarget:self action:@selector(NextAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.buttonForNext.frame = CGRectMake(324, 150, 50, 50);
    
    [self.playerView addSubview:self.buttonForNext];
    
    [_buttonForNext release];
}




#pragma mark 添加播放器
-(void)addAVPlayer
{
    NSURL *url=[NSURL URLWithString:self.arrUrl_m3u8[0]];
    
    //创建视频项目对象
    self.playerItem = [[AVPlayerItem alloc] initWithURL:url];
    
    //创建视频播放器对象
    self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
    
    [_playerItem release];
}




#pragma mark 播放结束后的代理回调
- (void)moviePlayDidEnd:(NSNotification *)notify
{
    self.imgViewForMusic.layer.speed = 0;
    
    self.isPlay = NO;
    
    [self.buttonForPlay setBackgroundImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Animation" object:nil];
    
    [self.player seekToTime:kCMTimeZero completionHandler:^(BOOL finished) {
        [self.progressSlider setValue:0.0 animated:YES];
    }];
}



#pragma mark 添加进度观察 - addProgressObserver
- (void)addObservers
{
    //注册观察者用来观察，是否播放完毕
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    
    // 监听loadedTimeRanges属性
    [self.playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
    
    __block ViewControllerForRadioCollectPlay *vc = self;
    
    //设置(1,1) 设置每秒执行一次
    self.playbackTimeObserver = [vc.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue: dispatch_get_main_queue() usingBlock:^(CMTime time) {
        
        //NSLog(@"%lld, %d", time.value, time.timescale);
        //总时长 将CMTime类型转成float类型
        CGFloat duration = CMTimeGetSeconds(self.playerItem.duration);
        
        //当前时间
        CGFloat current = CMTimeGetSeconds(self.player.currentTime);
        
        //倒计时
        CGFloat rem = duration - current;
        
        NSString *totalT = [NSString stringWithFormat:@"%02d:%02d",(int)rem/60 ,(int)rem%60];
        
        NSString *currenT = [NSString stringWithFormat:@"%02d:%02d",(int)current/60 ,(int)current%60];
        
        self.labelForStarTime.text = currenT;
        
        self.lableForEndTime.text = totalT;
        
        //保存总时长用于手动快进和快退
        self.totalMovieDuration = duration;
        
        
        //设置进度值 当前时间/总时长
        self.progressSlider.value = current/duration;
        
    }];
}


#pragma mark 销毁观察者
-(void)removeObservers
{
    [self.player removeTimeObserver:self.playbackTimeObserver];
}




#pragma mark KVO方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"loadedTimeRanges"])
    {
        // 计算缓冲进度
        NSTimeInterval timeInterval = [self availableDuration];
        
        CMTime duration = _playerItem.duration;
        
        CGFloat totalDuration = CMTimeGetSeconds(duration);
        
        [self.progressViewForRadioCache setProgress:timeInterval / totalDuration animated:YES];
    }
}



#pragma mark 计算缓存进度
- (NSTimeInterval)availableDuration
{
    NSArray *loadedTimeRanges = [[self.player currentItem] loadedTimeRanges];
    
    // 获取缓冲区域
    CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];
    
    float startSeconds = CMTimeGetSeconds(timeRange.start);
    
    float durationSeconds = CMTimeGetSeconds(timeRange.duration);
    
    // 计算缓冲总进度
    NSTimeInterval result = startSeconds + durationSeconds;
    
    return result;
}





#pragma mark 拖动进度条所触发的方法
- (void)ProgressAction:(UISlider *)sender
{
    //获取当前时间
    double currentTime = self.totalMovieDuration * sender.value;
    
    CMTime dragedCMTime = CMTimeMake(currentTime, 1);
    
    [self.player seekToTime:dragedCMTime completionHandler:^(BOOL finished)
     {
         [self.player play];
         
     }];
}




#pragma mark 点击后退Button所触发的方法
- (void)backButton:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        [self.player pause];
    }];
}




#pragma mark 点击播放Button所触发的方法
- (void)playButton:(UIButton *)button
{
    
    if (self.isPlay)
    {
        //获得当前旋转的时间点
        CFTimeInterval stopTime = [self.imgViewForMusic.layer convertTime:CACurrentMediaTime() fromLayer:nil];
        
        //设置播放速度为0,表示停止
        self.imgViewForMusic.layer.speed = 0;
        
        //记录当下的时间偏移量
        self.imgViewForMusic.layer.timeOffset = stopTime;
        
        
        [self.buttonForPlay setBackgroundImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
        
        [self.player pause];
        
    }else
    {
        //获得上次的时间偏移量
        CFTimeInterval stopTime = self.imgViewForMusic.layer.timeOffset;
        
        //设置速度1.0;
        self.imgViewForMusic.layer.speed = 1.0;
        
        
        //设置时间偏移量为0;
        self.imgViewForMusic.layer.timeOffset = 0;
        self.imgViewForMusic.layer.beginTime = 0;
        
        //设置开始时间
        self.imgViewForMusic.layer.beginTime = [self.imgViewForMusic.layer convertTime:CACurrentMediaTime() fromLayer:nil] - stopTime;
        
        [self.buttonForPlay setBackgroundImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
        
        
        if (self.equal== 0 && self.value == 0)
        {
            self.isEqual = NO;
            
            [self handleData];
            
            self.equal = self.cunt;
            
            self.value = 1;
        }
        
        
        if (self.cunt != self.equal)
        {
            self.isEqual = NO;
            
            [self handleData];
            
            self.equal = self.cunt;
        }
        
        if (self.isEqual)
        {
            [self.player play];
        }
        
        self.isEqual = YES;
    }
    
    [self.imgViewForMusic sd_setImageWithURL:[NSURL URLWithString:self.arrCover[0]]];
    
    self.isPlay = !self.isPlay;
    
    self.isOver = YES;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Animation" object:nil];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"xueer" object:nil];
}




#pragma mark 点击上一曲所触发的方法
- (void)ListAction:(UIButton *)button
{
    
    if (self.cunt > 0)
    {
        self.isPlay = NO;
        [self.buttonForPlay setBackgroundImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
        [self.player pause];
        
        self.cunt--;
        
        self.number = self.cunt;
        
        self.imgViewForMusic.layer.speed = 0.0;
        
        self.imgViewForMusic.image = [UIImage imageNamed:@"碟片.jpg"];
        
        if (self.isOver == YES)
        {
            [self.playerItem removeObserver:self forKeyPath:@"loadedTimeRanges" context:nil];
            
            self.isOver = NO;
        }
        
        self.progressSlider.value = 0;
        
        [self.progressViewForRadioCache setProgress:0. animated:NO];
        
        self.labelForStarTime.text = @"00 : 00";
        
        self.lableForEndTime.text = @"00: 00";
        
        
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"xueer" object:nil];
        
        //        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.cunt inSection:0];
        //
        //        [self.tabView scrollToRowAtIndexPath:indexPath atScrollPosition:  UITableViewScrollPositionMiddle animated:YES];
    }
}





#pragma mark 点击下一曲所触发的方法
- (void)NextAction:(UIButton *)button
{

    if (self.cunt < self.arrForCollect.count - 1)
    {
        self.isPlay = NO;
        [self.buttonForPlay setBackgroundImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
        
        [self.player pause];
        
        self.cunt++;
        
        self.number = self.cunt;
        
        self.imgViewForMusic.layer.speed = 0.0;
        
        self.imgViewForMusic.image = [UIImage imageNamed:@"碟片.jpg"];
        
        
        
        if (self.isOver == YES)
        {
            [self.playerItem removeObserver:self forKeyPath:@"loadedTimeRanges" context:nil];
            
            self.isOver = NO;
        }
        
        self.progressSlider.value = 0;
        
        [self.progressViewForRadioCache setProgress:0. animated:NO];
        
        self.labelForStarTime.text = @"00 : 00";
        
        self.lableForEndTime.text = @"00: 00";
        
        //        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.cunt inSection:0];
        //
        //        [self.tabView scrollToRowAtIndexPath:indexPath atScrollPosition:  UITableViewScrollPositionMiddle animated:YES];
        
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"xueer" object:nil];
    }
}




#pragma mark 添加旋转动画 discImageView
- (void)setdiscImageView
{
    //添加动画对象 改变 View z 轴的值
    self.animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    //旋转的起始值,从0点开始旋转
    self.animation.fromValue = [NSNumber numberWithInt:0];
    
    //最终旋转的角度是360度
    self.animation.toValue = [NSNumber numberWithInt:M_PI * 2];
    
    //旋转时长
    self.animation.duration = 10;
    
    //重复次数
    self.animation.repeatCount = NSIntegerMax;
    
    //旋转结束后是否要逆向返回原位置
    self.animation.autoreverses = NO;
    
    //是否按照结束位置继续旋转
    self.animation.cumulative = YES;
    
    //给View的layer层添加动画
    [self.imgViewForMusic.layer addAnimation:self.animation forKey:@"basicAnimation"];
    
    self.imgViewForMusic.layer.speed = 0;
}





#pragma mark 创建TabView
- (void)createTabView
{
    self.tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 300, SCREEN_WIDTH, SCREEN_HEIGHT - 300) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tabView];
    
    self.tabView.dataSource = self;
    
    self.tabView.delegate = self;
    
    //去掉分割线
    self.tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tabView registerClass:[CellForRadioPlayList class] forCellReuseIdentifier:@"pool1"];
}




#pragma mark UITableViewDataSource 的协议方法 >>>>>>
#pragma mark 相应分区的行的个数 (required 1/2)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrForCollect.count;
}




#pragma mark 给每行添加cell (required 2/2)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CellForRadioPlayList *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    cell.tag = indexPath.row;
    
    ModelForRadioCollect *model = [self.arrForCollect objectAtIndex:indexPath.row];
    
    //去掉选择cell之后背景颜色灰的效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.lableForTitle.numberOfLines = 0;
    
    cell.lableForTitle.textColor = [UIColor blackColor];
    
    cell.lableForTitle.font = [UIFont systemFontOfSize:15];
    
    cell.lableForTitle.text = model.title;
    
    
    
    cell.lableForPtime.text = model.ptime;
    
    cell.lableForPtime.textColor = [UIColor blackColor];
    
    cell.lableForPtime.font = [UIFont systemFontOfSize:15];
    
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.lableForNumber.alpha = 1;
        
    }];
    
    
    cell.lableForNumber.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    
    if (self.isPlay)
    {
        [cell.imgViewForAnimation startAnimating];
    }
    else
    {
        [cell.imgViewForAnimation stopAnimating];
    }
    
    
    
    if (cell.tag == self.cunt)
    {
        [cell.lableForTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        cell.lableForTitle.textColor = [UIColor redColor];
        
        cell.lableForPtime.textColor = [UIColor blueColor];
        
        [UIView animateWithDuration:0.5 animations:^{
            
            cell.lableForNumber.alpha = 0;
            
        }];
    }
    
    return cell;
}



#pragma mark UITableViewDelegate 的协议方法 >>>>>>
#pragma mark 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}






#pragma mark 选择相应的行所触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    CellForRadioPlayList *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    [cell.lableForTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
    
    cell.lableForTitle.textColor = [UIColor redColor];
    
    cell.lableForPtime.textColor = [UIColor blueColor];
    
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.lableForNumber.alpha = 0;
        
    }];
    
    [self.tabView reloadData];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"xueer" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        NSIndexPath *path = [NSIndexPath indexPathForRow:self.cunt inSection:0];
        
        [self.tabView reloadData];
        
        CellForRadioPlayList *cell1 = [tableView cellForRowAtIndexPath:path];
        
        [cell1.lableForTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        cell1.lableForTitle.textColor = [UIColor redColor];
        
        cell1.lableForPtime.textColor = [UIColor blueColor];
        
        
        
        
        
        
        
        [self.tabView scrollToRowAtIndexPath:path atScrollPosition:  UITableViewScrollPositionMiddle animated:YES];
        
        
        [UIView animateWithDuration:0.5 animations:^{
            
            cell1.lableForNumber.alpha = 0;
        }];
    }];
    
    
    
    
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"Animation" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note)
     {
         
         NSIndexPath *path = [NSIndexPath indexPathForRow:self.cunt inSection:0];
         
         CellForRadioPlayList *cell2 = [tableView cellForRowAtIndexPath:path];
         
         
         if (self.isPlay)
         {
             [cell2.imgViewForAnimation startAnimating];
         }
         else
         {
             [cell2.imgViewForAnimation stopAnimating];
         }
     }];
    
    
    
    if (self.cunt != indexPath.row)
    {
        self.isPlay = NO;
        
        [self.buttonForPlay setBackgroundImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
        
        self.imgViewForMusic.layer.speed = 0;
        
        self.imgViewForMusic.image = [UIImage imageNamed:@"碟片.jpg"];
        
        [self.player pause];
        
        
        if (self.isOver == YES)
        {
            [self.playerItem removeObserver:self forKeyPath:@"loadedTimeRanges" context:nil];
            
            self.isOver = NO;
        }
        
        self.progressSlider.value = 0;
        
        [self.progressViewForRadioCache setProgress:0. animated:NO];
        
        self.labelForStarTime.text = @"00 : 00";
        
        self.lableForEndTime.text = @"00: 00";
    }
    
    
    self.cunt = indexPath.row;
    
    self.number = self.cunt;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Animation" object:nil];
}


@end
